﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Chaine.Models;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using MongoDB.Bson;
using System.Web.Security;

namespace Chaine.DAO
{
    public static class AccountManager
    {
        private static MongoCollection<RegisterModel> _Registers;
        private static MongoCollection<RegisterModel> Registers
        {
            get
            {
                if (_Registers != null)
                    return _Registers;
                _Registers = DAO.DB.GetDBInstance().GetCollection<RegisterModel>("Registers");
                return _Registers;
            }
        }
        internal static List<RegisterModel> GetRegisters()
        {
            return Registers.Find(Query.EQ("IsDeleted", false)).OrderBy(m => m.UserName).ToList();
        }

        internal static bool CreateRegister(RegisterModel register)
        {
            return Registers.Insert<RegisterModel>(register).Ok;
        }

        internal static RegisterModel GetRegister(ObjectId id)
        {
            return Registers.FindOne(Query.EQ("_id", id));
        }

        internal static RegisterModel GetRegister(string username)
        {
            return Registers.FindOne(Query.EQ("UserName", username));
        }

        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="register"></param>
        /// <returns></returns>
        internal static bool UpdateRegister(RegisterModel register)
        {
            return Registers.Save<RegisterModel>(register).Ok;
        }

        /// <summary>
        /// 将用户的email表示为已验证
        /// </summary>
        /// <param name="id"></param>
        internal static bool ValidateEmail(ObjectId id)
        {
            var update = Update.Set("EmailIsValidated", true);
            return Registers.Update(Query.EQ("_id", id), update).Ok;
        }

        /// <summary>
        /// 设置管理员权限
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        internal static bool AddAdminRole(ObjectId id)
        {
            var member = GetRegister(id);
            if (member == null)
                return false;
            if (!Roles.RoleExists("admin"))
                Roles.CreateRole("admin");
            if (!Roles.IsUserInRole(member.UserName, "admin"))
                Roles.AddUserToRole(member.UserName, "admin");
            return true;
        }

        /// <summary>
        /// 移除管理员权限
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        internal static bool RemoveAdminRole(ObjectId id)
        {
            var member = GetRegister(id);
            if (member == null)
                return false;
            if (Roles.IsUserInRole(member.UserName, UserRole.Admin.ToString().ToLower()))
                Roles.RemoveUserFromRole(member.UserName, UserRole.Admin.ToString().ToLower());
            return true;
        }

        internal static bool IsAdmin(this MembershipUser user)
        {
            if (user == null)
                return false;
            return Roles.IsUserInRole(user.UserName, UserRole.Admin.ToString());
        }

        /// <summary>
        /// 删除会员信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        internal static bool DeleteMember(ObjectId id)
        {
            var member = GetRegister(id);
            if (member == null)
                return true;
            Membership.DeleteUser(member.UserName);
            Registers.Remove(Query.EQ("_id", id));
            return true;
        }

        /// <summary>
        /// 获取系统管理员
        /// </summary>
        /// <returns></returns>
        internal static List<MembershipUser> GetAdmins()
        {
            return Roles.GetUsersInRole("admin").Select(a => Membership.GetUser(a)).ToList();

        }
    }
}